﻿using System;
using UnityEngine;
using BridgeShips.GUISystem;
using System.Collections.Generic;
using UnityEngine.UI;

namespace BridgeShips.Control
{
    public class BuildingChooseShipTypesUI : GUIBehaviour
    {
        [SerializeField]
        private Window m_Window;

        [SerializeField]
        private Camera m_GUICamera;

        [Header("Audio")]
        [SerializeField]
        private SoundPlayer m_RefreshAudio;

        [SerializeField]
        private SoundPlayer m_SelectButtonAudio;

        private BuildingCategory[] m_Categories;

        private Button btn;
        // Use this for initialization
        void Start()
        {
            Player.ChooseShip.AddStartTryer(TryStart_ChooseShipUI);
            Player.ChooseShip.AddStopTryer(TryStop_ChooseShipUI);

            InventoryController.Instance.State.AddChangeListener(OnChanged_InventoryState);

            btn = transform.Find("ExitButton").GetComponent<Button>();
            btn.onClick.AddListener(BtnClick);
        }

        // Update is called once per frame
        void Update()
        {
            if (!m_Window.IsOpen)
                return;

            float scrollValue = Player.ScrollValue.Get();
        }
        public bool isVisable()
        {
            return Player.ChooseShip.Active;
        }
        
        private void OnChanged_InventoryState()
        {
            var inventoryClosed = InventoryController.Instance.IsClosed;
            if (!inventoryClosed)
            {
                while (Player.ChooseShip.Active)
                    Player.ChooseShip.TryStop();
            }
        }

        private bool TryStart_ChooseShipUI()
        {
            if (!InventoryController.Instance.IsClosed)
                return false;
            m_Window.Open();
            return true;
        }

        private bool TryStop_ChooseShipUI()
        {
            m_Window.Close();
            return true;
        }

        void BtnClick()
        {
            PlayerInputHandler.curActivity = null;
            GameController.LocalPlayer.ChooseShip.TryStop();
        }
    }
}
